
/**
 * 选择快递
 * @author Jiangdw
 * @createDate 2015-12-28
 * @param $
 */
;(function($, window, document, undefined){
	
	var url_, tabHtml,
		hotDelivery = '', abcdefg = '', hijklmn = '', opqrst = '', uvwxyz = '',
		_ag_reg = /^[a-gA-G]/, _hn_reg = /^[h-nH-N]/, _ot_reg = /^[o-tO-T]/, _uz_reg = /^[u-zU-Z]/,
		item, sShortName, firstLetter,
		_$a_g = new Array('A', 'B', 'C', 'D', 'E', 'F', 'G'),
		_$h_n = new Array('H', 'I', 'J', 'K', 'L', 'M', 'N'),
		_$o_t = new Array('O', 'P', 'Q', 'R', 'S', 'T'),
		_$u_z = new Array('U', 'V', 'W', 'X', 'Y', 'Z'),
		_$az = {hot:[], ABCDEFG:{A:[],B:[],C:[],D:[],E:[],F:[],G:[]}, HIJKLMN:{H:[],I:[],J:[],K:[],L:[],M:[],N:[]}, OPQRST:{O:[],P:[],Q:[],R:[],S:[],T:[]}, UVWXYZ:{U:[],V:[],W:[],X:[],Y:[],Z:[]}},
		
	
	_createTab = function($obj, onSelected){
		tabHtml = '<div class="ionTabs" id="__delivery" style="display:none" data-name="delivery"><ul class="ionTabs__head">';
		tabHtml += '<li class="ionTabs__tab" data-target="hotDelivery">热门物流</li>';
		tabHtml += '<li class="ionTabs__tab" data-target="abcdefg">ABCDEFG</li>';
		tabHtml += '<li class="ionTabs__tab" data-target="hijklmn">HIJKLMN</li>';
		tabHtml += '<li class="ionTabs__tab" data-target="opqrst">OPQRST</li>';
		tabHtml += '<li class="ionTabs__tab" data-target="uvwxyz">UVWXYZ</li></ul><div class="ionTabs__body">';
		
		$.getJSON(url_, function(data){
			for(var i in data){
				item = data[i];
				sShortName = item.sShortName;
				firstLetter = sShortName.substr(0, 1).toUpperCase();
				
				if(_ag_reg.test(sShortName)){
					_$az.ABCDEFG[firstLetter].push(item);
				} else if(_hn_reg.test(sShortName)) {
					_$az.HIJKLMN[firstLetter].push(item);
				}else if(_ot_reg.test(sShortName)) {
					_$az.OPQRST[firstLetter].push(item);
				} else if(_uz_reg.test(sShortName)){
					_$az.UVWXYZ[firstLetter].push(item);
				}
				if(i<16){
					_$az.hot.push(item);
				}
			}
			
			abcdefg = _buildItem(_$a_g, _$az.ABCDEFG);
			hijklmn = _buildItem(_$h_n, _$az.HIJKLMN);
			opqrst = _buildItem(_$o_t, _$az.OPQRST);
			uvwxyz = _buildItem(_$u_z, _$az.UVWXYZ);
			hotDelivery = _buildHotItem(_$az.hot);
			
			tabHtml += '<div class="ionTabs__item hotDelivery" data-name="hotDelivery"><table>' + hotDelivery + '</table></div>';
			tabHtml += '<div class="ionTabs__item" data-name="abcdefg"><table>' + abcdefg + '</table></div>';
			tabHtml += '<div class="ionTabs__item" data-name="hijklmn"><table>' + hijklmn + '</table></div>';
			tabHtml += '<div class="ionTabs__item" data-name="opqrst"><table>' + opqrst + '</table></div>';
			tabHtml += '<div class="ionTabs__item" data-name="uvwxyz"><table>' + uvwxyz + '</table></div>';
			tabHtml += '</div><div class="ionTabs__preloader"></div><div style="text-align:right"><a href="javascript:;" class="__close">[关闭]</a></div></div>';
			$('body').append(tabHtml);
			$('.__close').on('click', function(){
				$('#__delivery').hide();
			});
			$('#__delivery a.__item').on('click', function(){
				var _$item = $(this);
				$obj.val(_$item.text());
				if($.isFunction(onSelected)) {
					onSelected($obj, _$item);
				}
				$('#__delivery').hide();
			});
			$.ionTabs('#__delivery');
		});
		
	},
	
	_buildItem = function($warray, $iarray){
		var _item, ihtml = '';
		for(var x in $warray){
			if($iarray[$warray[x]].length < 1) continue;
			ihtml += '<tr><th>' + $warray[x] + '</th><td>';
			for(var m in $iarray[$warray[x]]){
				_item = $iarray[$warray[x]][m];
				ihtml += '<a href="javascript:;" class="__item" code="" sfullname="' + _item.sFullName + '" sshortname="' + _item.sShortName + '" companycode="' + _item.companyCode + '">' + _item.name + '</a>';
			}
			ihtml += '</td></tr>'
		}
		return ihtml;
	},
	
	_buildHotItem = function($iarray){
		var _item, ihtml = '<tr><td>';
		for(var x in $iarray){
			if(x>0 && x%8==0){
				ihtml += '</td></tr><tr><td>';
			}
			_item = $iarray[x];
			ihtml += '<a href="javascript:;" class="__item" code="" sfullname="' + _item.sFullName + '" sshortname="' + _item.sShortName + '" companycode="' + _item.companyCode + '">' + _item.name + '</a>';
		}
		ihtml += '</td></tr>';
		return ihtml;
	};
	
	$.fn.selectdelivery = function(options){
		var _$this = $(this),
			_$opts = $.extend({}, $.fn.selectdelivery.defaults, options);
		url_ = _$opts.url;
		var _$tag = $(_$opts.tag);
		
		_createTab(_$tag, _$opts.onSelected);
		
		_$tag.on('focus', function(){
			$('#__delivery').css({
				position: 'absolute',
				top: _$tag.offset().top + 20 + 'px',
				left: _$tag.offset().left + 'px'
			});
			$('#__delivery').show();
		});
		
		return this;
	};
	
	$.fn.selectdelivery.defaults = {
		tag: '.delivery',
		url: '/js/plugin/selectdelivery/delivery-json.js',
		onSelected: null
	};
	
})(jQuery, window, document);